package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.entity.Comment;
import com.entity.Document;
import com.entity.Idea;
import com.entity.Trend;
import com.entity.User;
import com.util.DBHelp;
import com.util.DateHelp;
import com.util.RowMapper;

public class CommentDao {
	private DBHelp<Comment> db=new DBHelp<Comment>();
	
	
	
	private class CommentRowMapper implements RowMapper<Comment>{

		public Comment mapRow(ResultSet rs) throws SQLException {
			Comment c=new Comment();
			c.setCreateTime(DateHelp.getTime());
			c.setId(rs.getString("id"));
			
			User u=new User();
			c.setUserid(rs.getString("userid"));
			u.setId(c.getUserid());
			u.setImage(rs.getString("image"));
			u.setUserName(rs.getString("username"));
			c.setUser(u);
			
			Idea i=new Idea();
			c.setIdeaid(rs.getString("ideaid"));
			i.setId(c.getIdeaid());
			i.setIdeaname(rs.getString("ideaname"));
			c.setIdea(i);
			
			Trend t=new Trend();
			c.setTrendid(rs.getString("trendid"));
			t.setId(c.getTrendid());
			t.setContent(rs.getString("content"));
			c.setTrend(t);
			
			Document d=new Document();
			c.setDocumentid(rs.getString("documentid"));
			d.setDocname(rs.getString("docname"));
			d.setId(c.getDocumentid());
			c.setDocument(d);
			return c;
		}
	}


	public List<Comment> findByDocmendidFromDocument(Comment c){
		String sql="select id,content,createtime,userid where docmentid=?";
		return db.executeQueryForList(sql, new CommentRowMapper(),c.getDocumentid());
	}
	
	public void saveFromTrend(Comment c) {
		String sql="insert into t_comment(id,message,createtime,userid) values (?,?,?,?) ";
		db.executeSQL(sql, c.getId(),c.getMessage(),c.getCreateTime(),c.getUserid());	
	}

	
	public void saveSayFromDoc(Comment c) {
		String sql="insert into t_comment(id,createtime.documentid,userid) values(?,?,?,?)";
		db.executeSQL(sql, c.getId(),c.getCreateTime(),c.getDocumentid(),c.getUserid());
	}
	
	public void delTrendByTrend(String id) {
		String sql="delete from t_comment where trendid=?";
		db.executeSQL(sql, id);
	}
	
	public List<Comment> findAllCommentFromTrend() {
		String sql="select tc.id,tc.message,tc.createtime,tc.userid,tc.trendid ,tu.image,tu.username from t_comment as tc left join t_user as tu on tc.userid=tu.id left join t_trend as tt on tc.trendid =tt.id  ";
		return db.executeQueryForList(sql, new CommentRowMapper());
	}

	public List<Comment> findByTrendid(String trendid) {
		String sql="select id,message,createtime,userid where trendid=?";
		return db.executeQueryForList(sql, new CommentRowMapper() , trendid);
	}

	public void saveMsgFromIdea(Comment c) {
		String sql = "insert into _comment(id,ideaid,message,userid) values (?,?,?,?)";
		db.executeSQL(sql, c.getId(),c.getIdeaid(),c.getMessage(),c.getUserid());
	} 

	public List<Comment> findMsgFromIdea() {
		String sql = "selecct tc.id,tc.message,tc.createtime,tc.ideaid,tu.userid FROM t_comment";
		return db.executeQueryForList(sql, new CommentRowMapper());
		
	}
}
